//
//  WishListViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/26.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "WishListViewController.h"

#import "MoreDetailViewController.h"
#import "WishTableViewCell.h"

@interface WishListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalPage;

@end

@implementation WishListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"心愿订单";
    
    self.dataArray = [NSMutableArray array];
    self.currentPage = 1;
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = ScreenX375(207);
    self.tableView.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
    [self.view addSubview:self.tableView];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 12; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d",i];
        [arr addObject:ImageWithName(imageName)];
    }
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [header setImages:arr forState:MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)headerRefresh {
    _currentPage = 1;
    [self getData];
    self.tableView.mj_footer.hidden = NO;
}

- (void)footerRefresh {
    if (_currentPage <= _totalPage) {
        
        _currentPage++;
        [self getData];
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
    }else{
        
//        [HttpManager showNoteMsg:@"已经到底啦"];
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.hidden = YES;
    }
}

- (void)getData {
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"userId":kNSUDefaultReadKey(@"userId"),@"pageSize":@"10",@"page":[NSNumber numberWithInteger:_currentPage]}];
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/getWishLish",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            if (self.currentPage == 1) {
                [self.dataArray removeAllObjects];
            }
            self.totalPage = [successResponse[@"data"][@"pages"] integerValue];
            NSError *error;
            for (NSDictionary *dic in successResponse[@"data"][@"list"]) {
                WishModel *model = [[WishModel alloc] initWithDictionary:dic error:&error];
                [self.dataArray addObject:model];
            }
            if (self.totalPage == self.currentPage) {
                self.tableView.mj_footer.hidden = YES;
            }
            if ([successResponse[@"data"][@"list"] count] == 0) {
                [HttpManager showNoteMsg:@"暂无更多数据"];
                self.tableView.mj_footer.hidden = YES;
            }
            UIButton *noteBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(76), ScreenX375(21))];
            [noteBtn setTitle:[NSString stringWithFormat:@"%@个订单",successResponse[@"data"][@"total"]] forState:UIControlStateNormal];
            [noteBtn setTitleColor:REXADECIMALCOLOR(@"#f21934") forState:UIControlStateNormal];
            noteBtn.titleLabel.font = Kfont(13);
            noteBtn.backgroundColor = [UIColor whiteColor];
            noteBtn.layer.cornerRadius = ScreenX375(15);
            noteBtn.clipsToBounds = YES;
            noteBtn.layer.borderColor = REXADECIMALCOLOR(@"#f21934").CGColor;
            noteBtn.layer.borderWidth = 1.0;
            
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:noteBtn];
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }else if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WishTableViewCell *cell = [WishTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.wishModel = _dataArray[indexPath.section];
    WishModel *model = _dataArray[indexPath.section];
    cell.buyClick = ^{
        MoreDetailViewController *vc = [[MoreDetailViewController alloc] init];
        vc.proId = model.proId;
        [self.navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScreenX375(211);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return ScreenX375(10);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(10))];
    view.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
    return view;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
